import Vue from 'vue'
import Vuex from 'vuex'
import { getItem, setItem } from '@/utils/storage'

Vue.use(Vuex)

const TOKEN_KEY = 'TOUTIAO_USER'

export default new Vuex.Store({
  state: {
    // 一个对象,存储当前登录用户信息(token)
    user: getItem(TOKEN_KEY),
    stores: {}, // 店铺
    shops: [],
    goodsLists: {}, //商品数据
    goodsInfo: {}, // 商品数据
    goodsCarList: [] // 购物车列表
  },
  mutations: {
    setUser (state, data) {
      state.user = data
      // 为了防止刷新丢失，我们需要把数据备份到本地存储
      setItem(TOKEN_KEY, state.user)
      window.localStorage.setItem(TOKEN_KEY, JSON.stringify(state.user))
    },
    // selectLocation (state, data) {
    //   state.selectLocation = data
    // },
    setStore (state, data) {
      state.stores = data
    },
    setShops (state, data) {
      state.shops = data
    },
    setGoodsLists (state, data) {
      state.goodsLists = data
    },
    setGoodsInfo (state, data) {
      state.goodsInfo = data
    },
    addGoodsCar (state, data) {
      state.goodsCarList.push(data)
    }
  },
  actions: {},
  modules: {}
})
